package com.example.indenture.race.service.impl;

import com.example.indenture.common.vo.Result;
import com.example.indenture.common.vo.Search;
import com.example.indenture.company.dao.CompanyDao;
import com.example.indenture.race.service.RaceService;
import com.example.indenture.race.dao.RaceDao;
import com.example.indenture.race.entity.Race;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author CY
 * @date 2023/02/21 15:09
 **/
@Service
public class RaceServiceImpl implements RaceService {
    @Autowired
    private RaceDao raceDao;
    @Resource
    private CompanyDao companyDao;

    @Override
    @Transactional
    public Integer insert(Race race) {
        //逻辑判断
        if (race == null) {
            Result.failed("Race cannot be null!");
        }

        race.setCreateDate(LocalDateTime.now());
        race.setUpdateTime(LocalDateTime.now());
        return raceDao.insert(race);

    }

    @Override
    public Integer existsAnotherRace(String name) {
        final Integer list = raceDao.selectIdByName(name);
        System.out.println(list);
        return list;
    }

    @Override
    @Transactional
    public Integer delete(Integer rid) {
        if (rid == null) {
            return 0;
        }
        return raceDao.delete(rid);
    }

    @Override
    @Transactional
    public Integer update(Race race) {
        return raceDao.update(race);
    }

    @Override
    @Transactional
    public Race getRaceById(Integer rid) {
        final Race race = raceDao.getRaceById(rid);
        race.setCompany(companyDao.selectCompanyById(race.getCid()));
        return race;
    }

    @Override
    @Transactional
    public PageInfo<Race> getAllRaces(Search search) {
        //初始化查询参数
        search.initSearch();
        //开启分页插件
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        //获取比赛页面并筛选已过期比赛.
        List<Race> races = raceDao.getAllRaces(search);
        races = races.stream().filter(race -> race.getDeadline().isAfter(LocalDateTime.now())).collect(Collectors.toList());
        //设置公司名称.
        for (Race race : races) {
            race.setCompany(companyDao.selectCompanyById(race.getCid()));
        }
//
//        return new PageInfo<>(Optional
//                .ofNullable(raceDao.getAllRaces(search))
//                .orElse(Collections.emptyList()));
        return new PageInfo<>(Optional
                .ofNullable(races)
                .orElse(Collections.emptyList()));
    }

    @Override
    @Transactional
    public PageInfo<Race> getRacesByUser(Integer uid, Search search) {
        search.initSearch();
        List<Race> races = raceDao.getRacesByUser(uid, search);
        //检测比赛是否过时
        races = races.stream().filter(race -> race.getDeadline().isAfter(LocalDateTime.now())).collect(Collectors.toList());
        //获取公司名称
        for (Race race : races) {
            race.setCompany(companyDao.selectCompanyById(race.getCid()));
        }
        //设置分页信息
        PageHelper.startPage(search.getCurrentPage(), search.getPageSize());
        return new PageInfo<>(Optional
                .ofNullable(races)
                .orElse(Collections.emptyList()));

    }

    @Override
    public Boolean isOutDate(LocalDateTime now, LocalDateTime it) {
        return now.isAfter(it);
    }

    @Override
    public List<Race> getListsByCompany(Integer cid) {
        return raceDao.getRacesByCompany(cid);
    }

    @Override
    public List<Race> getAllRaces() {
        return raceDao.getAllracess();
    }

}
